package com.witdata.common.service.system.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.witdata.mysql.base.service.TagBaseService;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.TagDto;
import com.witdata.mysql.domain.entity.TTag;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.mapper.SystemMapper;
import com.witdata.common.service.system.TagService;
import com.witdata.common.util.IdUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TagServiceImpl implements TagService {
    private final TagBaseService tagBaseService;

    @Override
    public List<TagDto> getTagList() {
        List<TTag> tTagList = tagBaseService.list();
        return SystemMapper.INSTANCE.toTagDtoList(tTagList);
    }

    @Override
    public List<TagDto> getTagListByIdList(List<Long> idList) {
        List<TTag> tTagList = tagBaseService.listByIds(idList);
        return SystemMapper.INSTANCE.toTagDtoList(tTagList);
    }

    @Override
    public void addTag(TagDto tagDto) {
        TTag tTag = SystemMapper.INSTANCE.toTTag(tagDto);
        tTag.setId(IdUtils.createId());
        tTag.setCreated(LocalDateTime.now());
        tTag.setUpdated(LocalDateTime.now());
        boolean save = tagBaseService.save(tTag);
        if (!save) throw new GlobalException(Status.SAVEERROR);
    }

    @Override
    public List<TagDto> getTagListByType(String type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type", type);
        List<TTag> tTagList = tagBaseService.list(queryWrapper);
        return SystemMapper.INSTANCE.toTagDtoList(tTagList);
    }

    @Override
    public TagDto getTagById(Long id) {
        TTag tTag = tagBaseService.getById(id);
        return SystemMapper.INSTANCE.toTagDto(tTag);
    }
}
